/**
 * 
 */
package org.shortestpath.core;

import org.shortestpath.core.util.DijkrastUtil;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;


/**
 * @author Thilani
 * 
 */
public class Dijkstrast {

    private Vector<Point> resultGraph;

    /**
     * Calculates the shortest path using dijkrast algorithm TODO need to crop
     * the vertexes vector for eliminate unnecessary vertexes
     * 
     * @param source
     *            The source vertex to start the calculation
     * @param destination
     *            The designation vertex
     * @param vertexes
     *            The vertexes vector
     * @return The vector that contains the vertexes needed for shortest path
     */
    public Vector<Point> calculateShortestPath(Point source, Point destination, Vector<Point> vertexes) {
        resultGraph = new Vector<Point>();
        DijkrastUtil.initSingleSource(vertexes, source);
        int intialSize = vertexes.size();
        for (int i = 0; i < intialSize; i++) {
            Point minimumVertex = DijkrastUtil.extractMin(vertexes);
            vertexes.remove(minimumVertex);

            // for each vertexes adj minimum vertex perform relax
            List<Point> adjacentVertexes = minimumVertex.getAdjacentNodes();
            Iterator<Point> iterator = adjacentVertexes.iterator();
            while (iterator.hasNext()) {
                DijkrastUtil.doRelax(minimumVertex, iterator.next());
            }

        }
        resultGraph.addElement(destination);
        Point fromNode = destination.getFrom_node();
        while (fromNode != null) {
            resultGraph = processResultGraph(resultGraph, fromNode);
            fromNode = fromNode.getFrom_node();
        }

        return resultGraph;
    }

    /**
     *
     * @param graph
     * @param destination
     * @return
     */
    private Vector<Point> processResultGraph(Vector<Point> graph, Point destination) {
        resultGraph.addElement(destination);
        return resultGraph;
    }

}
